/* 
 * Copyright (C) 2011, WordCards4Me Project - http://code.google.com/p/wordcards4me/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.wordcards4me;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;

/**
 * This class represent screen for application configuration.
 */
public class SettingsScreen extends Form implements CommandListener {

	public static final String FONT_PROPERTY = "font";

	private Command saveCommand = new Command("Save", Command.ITEM, 1);

	private ValuedChoiceGroup fontSizeGroup;
	private ValuedChoiceGroup fontStyleGroup;
	private ValuedChoiceGroup fontFaceGroup;
	
	private ValuedChoiceGroup orientationGroup;
	

	public SettingsScreen() {
		super("Fonts");

		addCommand(saveCommand);
		setCommandListener(this);

		append(getFontSizeGroup());
		append(getFontStyleGroup());
		append(getFontFaceGroup());
		append(getOrientationGroup());
	}

	public void commandAction(Command command, Displayable screen) {
		if (command == saveCommand) {
			CardMidlet.setProperty("wc4me.font.face",
					((Integer)fontFaceGroup.getSelectedValue()));
			CardMidlet.setProperty("wc4me.font.style",
					((Integer)fontStyleGroup.getSelectedValue()));
			CardMidlet.setProperty("wc4me.font.size",
					((Integer)fontSizeGroup.getSelectedValue()));
			CardMidlet.goBackward();
		}
	}

	private ValuedChoiceGroup getOrientationGroup() {
		if (orientationGroup == null) {
			final Vector types = new Vector();
			types.addElement(new Choice("Portrait", new Integer(CardCanvas.PORTRAIT)));
			types.addElement(new Choice("Landscape", new Integer(CardCanvas.LANDSCAPE)));
			orientationGroup = new ValuedChoiceGroup("Orientation", types);
		}
		return orientationGroup;
	}
	
	private ValuedChoiceGroup getFontSizeGroup() {
		if (fontSizeGroup == null) {
			final Vector sizes = new Vector();
			sizes.addElement(new Choice("Small", new Integer(Font.SIZE_SMALL)));
			sizes.addElement(new Choice("Medium", new Integer(Font.SIZE_MEDIUM)));
			sizes.addElement(new Choice("Large", new Integer(Font.SIZE_LARGE)));
			fontSizeGroup = new ValuedChoiceGroup("Font Size", sizes);
		}
		return fontSizeGroup;
	}
	
	private ValuedChoiceGroup getFontStyleGroup() {
		if (fontStyleGroup == null) {
			final Vector styles = new Vector();
			styles.addElement(new Choice("Plain", new Integer(Font.STYLE_PLAIN)));
			styles.addElement(new Choice("Bold", new Integer(Font.STYLE_BOLD)));
			styles.addElement(new Choice("Italic", new Integer(Font.STYLE_ITALIC)));
			fontStyleGroup = new ValuedChoiceGroup("Font Style", styles);
		}
		return fontStyleGroup;
	}

	private ValuedChoiceGroup getFontFaceGroup() {
		if (fontFaceGroup == null) {
			final Vector faces = new Vector();
			faces.addElement(new Choice("System", new Integer(Font.FACE_SYSTEM)));
			faces.addElement(new Choice("Monospace", new Integer(Font.FACE_MONOSPACE)));
			faces.addElement(new Choice("Proportional", new Integer(Font.FACE_PROPORTIONAL)));
			fontFaceGroup = new ValuedChoiceGroup("Font Face", faces);
		}
		return fontFaceGroup;
	}
	
	/**
	 * This class extends {@link ChoiceGroup} class for association of items 
	 * which are shown on the screen to values corresponding to them.
	 */
	private class ValuedChoiceGroup extends ChoiceGroup {

		private Vector items;
		
		public ValuedChoiceGroup(final String title, final Vector items) {
			super(title, ChoiceGroup.POPUP);
			this.items = items;

			Enumeration e = items.elements();
			while (e.hasMoreElements()) {
				append(((Choice) e.nextElement()).getKey(), null);
			}
		}

		public Object getSelectedValue() {
			return ((Choice) items.elementAt(getSelectedIndex())).getValue();
		}
	}
	
	private final class Choice {
		private final String key;
		private final Object value;

		public Choice(String key, Object value) {
			this.key = key;
			this.value = value;
		}

		public String getKey() {
			return key;
		}

		public Object getValue() {
			return value;
		}
	}
}
